#include "Common.h"
#include "String.h"
#include "SharedPtr.h"
#include "ResourceManager.h"
#include "Database/DatabaseEnv.h"
#include "StringConverter.h"
#include "NGLog.h"
#include "MersenneTwister.h"
#include "ScriptMgr.h"
#include "Threading/Mutex.h"
#include "Towns.h"
#include "Game.h"
#include "Util.h"
#include "Character.h"
#include "CharManager.h"
#include "WSSocket.h"
#include "WorldPacket.h"
#include "Message.h"
#include "GameThreadLogic.h"
#include "AIInterface.h"
#include "CenterBankManager.h"
#include "CenterBattleManager.h"
#include "GameDataEnum.h"
#include "GameDefine.h"
#include "GameStruct.h"
#include LOGIC_MGR_PATH
#include XMLDATA_MGR_PATH

using namespace AIScript;	

//-----------------------------------------------------------------------
ChannelCreateThread::ChannelCreateThread(ResourceProxy town)
{
	m_townProxy = town;
}

//-----------------------------------------------------------------------
bool ChannelCreateThread::run(void *p)
{
#ifndef _NOUSE_DEFAULT_CHANNEL_ASSIGN__
	sGLMgr.CreateChannel(m_townProxy._handle, (std::set<ResourceProxy> *)p);
#endif
	return true;
}

LockedQueue<UserAuthenticInfo> DealAuthenticationQueueThread::g_authenQueue;
//-----------------------------------------------------------------------
DealAuthenticationQueueThread::DealAuthenticationQueueThread(void)
{
	uint32 threadnum = sXmlDataMgr.GetConfXMLValue("SYN_DISPOSE_THREAD");
	m_QueueCreatePool.Startup(new UserAuthenThreadRunable(), threadnum, threadnum, 0, "authenticationCreateThread"); 
}
//-----------------------------------------------------------------------
bool DealAuthenticationQueueThread::run(void)
{
	while(ResourceManager::isWorldLoading()) 
	{ 
		sleep(2); 
	}

	UserAuthenticInfo * pInfo = NULL;	
	while(true)
	{
		if(g_authenQueue.size() && sGLMgr.m_authen_queue_count.GetVal() <= sXmlDataMgr.GetConfXMLValue("SYN_DISPOSE_NUM"))
		{
			
			pInfo = new UserAuthenticInfo();		
		
			if(g_authenQueue.next_fetch(pInfo))
			{
				m_QueueCreatePool.addQueue(pInfo);
			
			}
			else
			{
				delete pInfo;
			}
			pInfo = NULL;
		}
		
		Sleep(1);
	}
	
	return true;
}

//-----------------------------------------------------------------------
bool UserAuthenThreadRunable::run(void *p)
{
	UserAuthenticInfo* info = (UserAuthenticInfo*)p;
	if(info)
	{
		if(info->pSocket.isNull())
			return true;
	}
	
	++ sGLMgr.m_authen_queue_count ;
	switch(info->nLogic)
	{
	case en_UserLoginLogic_First:
		sGLMgr.SelectLoginServer(info);
		break;
	case en_UserLoginLogic_Second:
		sGLMgr.DisposeSceondLogin(info);
		break;
	default:
		sGLMgr.WorldUserAuthentication(info);
		break;
	}
	-- sGLMgr.m_authen_queue_count ;
	
	return true;
}

/************************************** 通用排行 ***********************************/
//------------------------------------------
LoadPlayerCommonTop::LoadPlayerCommonTop(const uint32 topType, const uint32 gzId):m_topType(topType), m_gzId(gzId)
{
	m_lstQT.clear();
}
//------------------------------------------
bool LoadPlayerCommonTop::run()
{
	stOrderInfo stOI = sXmlDataMgr.GetOrderValueKey(m_topType);
	if(stOI.orderValu_key == "")
		return false;
	
	uint32 topNum = stOI.maxPlayerNum;
	String fieldName = stOI.orderValu_key;
	String field2Name = stOI.orderValu_key2;
	
	String sql_order = "";
	char buf[256] = "",buf2[256] = "";
	if(fieldName != "")
	{
		sprintf(buf,"`%s` desc",fieldName.c_str());
		sql_order += buf;
	}
	
	if(field2Name != "")
	{
		sprintf(buf2,",`%s` desc",field2Name.c_str());
		sql_order += buf2;
	}
	
	list<ResourcePtr> lstChars;
	if (m_gzId == 0)
	{
		sCharMgr.getDBObjects(&lstChars, 0, topNum, "`isnpc` = 0 and `%s` > 0 order by `%s` desc", fieldName.c_str(), fieldName.c_str());
	}
	else
	{
		sCharMgr.getDBObjects(&lstChars, 0, topNum, "`gz_id` = %u and `isnpc` = 0 and `%s` > 0 order by `%s` desc", m_gzId, fieldName.c_str(), fieldName.c_str());
	}
	
	list<ResourcePtr>::iterator iter = lstChars.begin();
	stCommonQueueTop qt;
	for (; iter != lstChars.end(); ++iter)
	{
		qt.id = (*iter)->getUInt32Field("serial");
		UserPtr pUser = sUserMgr.getDBObjectByHandle((*iter)->getUInt32Field("userid"));
		if(!pUser.isNull())
		{
			qt.pid = pUser->getUInt32Field("platform_id");
			qt.addr = pUser->getStringField("addr");
			GameBank pBank;
			if(sBankMgr.GetRealBank(qt.pid, pBank) == 1)
				qt.sign_name = pBank.sdata2;
		}
		
		sGLMgr.FilterChars((*iter)->getStringField("name"), qt.name);
		qt.head = (*iter)->getStringField("desc");
		qt.data = (*iter)->getUInt32Field(fieldName);
		qt.sex = (*iter)->getUInt32Field("sex");
		qt.trend = 2;
		m_lstQT.push_back(qt);
	}
	
	// Log.Notice("LoadPlayerCommonTop","m_gzId[%u] m_topType[%u] size[%u]", m_gzId, m_topType, m_lstQT.size());
	
	sGLMgr.UpdateCommonPlayerTop(m_gzId, m_topType, m_lstQT);
	return true;
}

CreateClubRoomBattleListThread::CreateClubRoomBattleListThread(String & strPlayMode, const uint32 & count)
{
	m_strPlayMode = strPlayMode;
	m_nCreateCount = count;
}

//------------------------------------------
bool CreateClubRoomBattleListThread::run()
{// 开启线程
	for(uint8 i=0; i<40; ++i)
	{
		sCenterBattleMgr.SystemCreateCenterBattle(m_strPlayMode);
		Sleep(1000);
	}
	
	// sCenterBattleMgr.SystemCreateCenterBattle(m_strPlayMode);
	return true;
}



